package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author liuyandeng
 * @date 2023/8/12
 * @Description
 */

@Controller
@ResponseBody
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUser") // localhost:8080/getUser
    public UserInfo getUserById(Integer id) {
        UserInfo userInfo = null;
        if (id != null && id > 0) {
            userInfo = userService.getUserByid(id);
        }
        return userInfo;
    }

    @RequestMapping("/reg") // localhost:8080/reg
    public HashMap<String,Object> reg(String username,String password) {
        HashMap<String ,Object> result = new HashMap<>();
        int data = 0; // 1 为注册成功
        String msg = "未知错误";
        // 1、得到并验证前端的参数
        if (StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
            // 2、方法数据库执行操作
            data = userService.reg(username,password);
        } else {
            msg = "非法参数";
        }
        // 3、将操作结果返回给前端
        result.put("succ",200);
        result.put("data",data);
        result.put("msg",msg);
        return result;
    }

}
